package com.oilsection.demo.quartz;



import com.oilsection.demo.dao.*;
import com.oilsection.demo.po.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class scheduleTask {
    @Autowired
    private GetQkListSourceDao getQkListSourceDao;

    @Autowired
    private GetOverviewInfoSourceDao getOverviewInfoSourceDao;

    @Autowired
    private GetQkListSourceDao getGetQkListSourceDao;

    @Autowired
    private GetJzListSourceDao getJzListSourceDao;

    @Autowired
    private GetJcdListSourceDao getJcdListSourceDao;

    @Autowired
    private GetCkListSourceDao getCkListSourceDao;

    @Autowired
    private GetHkListSourceDao getHkListSourceDao;

    @Autowired
    private GetJkxlListSourceDao getJkxlListSourceDao;

    @Autowired
    private GetXldListSourceDao getXldListSourceDao;

    @Autowired
    private GetJcEventListSourceDao getJcEventListSourceDao;



    private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static DateFormat dateFormat1 = new SimpleDateFormat("yyyyMMdd");

    /**
     * 添加定时任务（使用corn表达式设置定时任务执行时间，例如 10秒：0/10 * * * * ?）
     * 可直接指定时间间隔，例如：5秒 @Scheduled(fixedRate=5000)
     */
    /**更新总览**/
    @Scheduled(cron = "0/10 * * * * ?")
    private void updateQkoverviewinfoTable() {
        System.out.println("updateQkoverviewinfoTable");
        System.out.println("更新总览");
        List<KqInfoTable> kqInfoTables = getQkListSourceDao.findAll();
        List<QkoverviewinfoTable> qkoverviewinfoTables = getOverviewInfoSourceDao.findAll();
        int jzSum =  kqInfoTables.stream().mapToInt(map -> Integer.parseInt(map.getJzNum().toString())).sum();
        int jcdSum = kqInfoTables.stream().mapToInt(map -> Integer.parseInt(map.getJcdNum().toString())).sum();
        int cqxlSum = kqInfoTables.stream().mapToInt(map -> Integer.parseInt(map.getCqkNum().toString())).sum();
        int xlkSum = kqInfoTables.stream().mapToInt(map -> Integer.parseInt(map.getXlkNum().toString())).sum();
        int hkSum = kqInfoTables.stream().mapToInt(map -> Integer.parseInt(map.getHkNum().toString())).sum();
        int areaSum = kqInfoTables.stream().mapToInt(map -> Integer.parseInt(map.getArea().toString())).sum();
        for (int i = 0;i< qkoverviewinfoTables.size();i++){
            QkoverviewinfoTable temp = qkoverviewinfoTables.get(i);
            temp.setKqNum(kqInfoTables.size());
            temp.setJzNum(jzSum);
            temp.setJcdNum(jcdSum);
            temp.setCqxlNum(cqxlSum);
            temp.setQkxlNum(xlkSum);
            temp.setHkxlNum(hkSum);
            temp.setKqArea(areaSum);
            getOverviewInfoSourceDao.save(temp);
        }

    }

    @Scheduled(cron = "0/10 * * * * ?")
    private void updateKqinfoTable(){
        System.out.println("updateKqinfoTable");
        List<KqInfoTable> kqInfoTables = new ArrayList<>();
        kqInfoTables = getQkListSourceDao.findAll();
        for (int i = 0;i<kqInfoTables.size();i++){
            KqInfoTable temp = kqInfoTables.get(i);
            List<JzInfoTable> jzInfoTables = getJzListSourceDao.findByKqId(temp.getId());
            int jcdSum = jzInfoTables.stream().mapToInt(map -> Integer.parseInt(map.getJcdNum().toString())).sum();
            int ckSum = jzInfoTables.stream().mapToInt(map -> Integer.parseInt(map.getCkNum().toString())).sum();
            int xldSum = jzInfoTables.stream().mapToInt(map -> Integer.parseInt(map.getXldNum().toString())).sum();
            int hkSum = jzInfoTables.stream().mapToInt(map -> Integer.parseInt(map.getHkNum().toString())).sum();
            int areaSum = jzInfoTables.stream().mapToInt(map -> Integer.parseInt(map.getJzArea().toString())).sum();
            String jzState = "正常";
            Date date = new Date();

            for (int j = 0;j<jzInfoTables.size();j++){
                JzInfoTable ele = jzInfoTables.get(j);
                if("异常".equals(ele.getJzState())){
                    jzState = "异常";
                }
            }
            temp.setJzNum(jzInfoTables.size());
            temp.setJcdNum(jcdSum);
            temp.setCqkNum(ckSum);
            temp.setXlkNum(xldSum);
            temp.setHkNum(hkSum);
            temp.setArea(areaSum);
            temp.setKqState(jzState);
            temp.setUpdateTime(date);
            getQkListSourceDao.save(temp);
        }
    }

    @Scheduled(cron = "0/10 * * * * ?")
    private void  updateJzinfoTable(){
        System.out.println("updateJzinfoTable");
        List<JzInfoTable> jzInfoTables = getJzListSourceDao.findAll();

        for (int i = 0; i<jzInfoTables.size();i++){
            JzInfoTable temp = jzInfoTables.get(i);
            List<JcdInfoTable> jcdInfoTables = getJcdListSourceDao.findByJzId(temp.getId());
            List<CkInfoTable> ckInfoTables = getCkListSourceDao.findByCkyIdOrSrId(temp.getId(),temp.getId());
            List<HkInfoTable> hkInfoTables = getHkListSourceDao.findByJzId(temp.getId());
            List<JkxlInfoTable> jkxlInfoTables = getJkxlListSourceDao.findByXljzId(temp.getId());
            List<XldInfoTable> xldInfoTables = getXldListSourceDao.findByJzId(temp.getId());
            Date date = new Date(System.currentTimeMillis());
            if(ckInfoTables != null){
                temp.setCkNum(ckInfoTables.size());
            }
            if (hkInfoTables != null){
                temp.setHkNum(hkInfoTables.size());
            }
            if (jkxlInfoTables != null){
                temp.setXldNum(xldInfoTables.size());
            }

            temp.setJcdNum(jcdInfoTables.size());
            temp.setUpdateTime(date);
            getJzListSourceDao.save(temp);
        }
    }

//    @Scheduled(cron = "0/10 * * * * ?")
    private void updateXldTable(){
        System.out.println("updateXldTable");
        List<JcEventInfoTable> jcEventInfoTables = getJcEventListSourceDao.findAll();
        for (int i = 0;i<jcEventInfoTables.size();i++){
            JcEventInfoTable temp = jcEventInfoTables.get(i);
            XldInfoTable xldTemp = new XldInfoTable();
            HkInfoTable hkTemp = new HkInfoTable();
            JkxlInfoTable jkTemp = new JkxlInfoTable();
            CkInfoTable ckTemp = new CkInfoTable();
            if("SF6".equals(temp.getSzjType())){
                xldTemp.setId("XL"+dateFormat1.format(temp.getUpdateTime())+temp.getJzId());
                xldTemp.setJzId(temp.getJzId());
                xldTemp.setDealSection("未处理");
                xldTemp.setXldcXpoint(temp.getJcXpoint());
                xldTemp.setXldcYpoint(temp.getJcYpoint());
                xldTemp.setXldsXpoint(temp.getJcXpoint());
                xldTemp.setXldsYpoint(temp.getJcYpoint());
                xldTemp.setFindTime(temp.getUpdateTime());
                xldTemp.setJcValue(temp.getJcValue());
                getXldListSourceDao.save(xldTemp);
            }

            if("井筒".equals(temp.getJcType()) && temp.getJcValue() > 0){
                hkTemp.setId("HK"+dateFormat1.format(temp.getUpdateTime())+temp.getJzId());
                hkTemp.setJzId(temp.getJzId());
                hkTemp.setDealSection("未处理");
                hkTemp.setJzXpoint(temp.getJcXpoint());
                hkTemp.setJzYpoint(temp.getJcYpoint());
                hkTemp.setFindTime(temp.getUpdateTime());
                hkTemp.setJcValue(temp.getJcValue());
                getHkListSourceDao.save(hkTemp);
            }

            if ("井口".equals(temp.getJcType()) && temp.getJcValue() > 0){
                jkTemp.setId("JKXL"+dateFormat1.format(temp.getUpdateTime())+temp.getJzId());
                jkTemp.setXljzId(temp.getJzId());
                jkTemp.setDealSection("未处理");
                jkTemp.setFindTime(temp.getUpdateTime());
                jkTemp.setJcValue(temp.getJcValue());
                getJkxlListSourceDao.save(jkTemp);
            }
            JzInfoTable curJz = getJzListSourceDao.findById(temp.getJzId()).get();
            if ("库内气体".equals(temp.getJcType()) && temp.getJcValue() > 0 && curJz.getUpdateFlag() == null){
                List<JzInfoTable> jzInfoTableList = getJzListSourceDao.findByUpdateFlagNotNull();
                Double dMin = 0.0;
                Integer dIndex = 0;

                for (int j = 0; j<jzInfoTableList.size();j++){
                    JzInfoTable jzTemp = jzInfoTableList.get(j);
                    if (curJz.getId() != jzTemp.getId()){
                       Double dTemp = Math.sqrt(Math.pow(Math.abs(Double.valueOf(curJz.getJkXpoint())-Double.valueOf(jzTemp.getJkXpoint())),2)+Math.pow(Math.abs(Double.valueOf(curJz.getJkYpoint())-Double.valueOf(jzTemp.getJkYpoint())),2));
                       if(dTemp <= dMin){
                           dMin = dTemp;
                           dIndex = j;
                       }
                    }
                }
                if (jzInfoTableList.size() > 0) {
                    JzInfoTable oozeJz = jzInfoTableList.get(dIndex);
                    if (oozeJz != null) {
                        ckTemp.setId("CK" + dateFormat1.format(temp.getUpdateTime()) + temp.getJzId());
                        ckTemp.setCkyId(curJz.getId());
                        ckTemp.setSrId(oozeJz.getId());
                        jkTemp.setDealSection("未处理");
                        jkTemp.setFindTime(temp.getUpdateTime());
                        jkTemp.setJcValue(temp.getJcValue());
                        getCkListSourceDao.save(ckTemp);

                    }
                }
            }

        }

    }


}
